package com.itheima.leetcode;

/**
 * <h2>买卖股票的最佳时机 II - 贪心</h2>
 * <h3>某一天买入股票，未来任意一天卖出，只能卖了再买，但可以同一天卖出后再买入，求最大利润</h3>
 * <h4>有利润就买卖，只看眼前</h4>
 */
public class SharesIILeetcode122 {

    public static int maxProfit(int[] prices) {
        int i = 0;
        int j = 1;
        int sum = 0;
        while (prices.length > j) {
            if (0 < prices[j] - prices[i]) { // 有利润
                sum += prices[j] - prices[i];
            }
            i++;
            j++;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
        System.out.println(maxProfit(new int[]{1, 2, 3, 4, 5}));
        System.out.println(maxProfit(new int[]{7, 6, 4, 3, 1}));
    }
}
